//
//  MMThemeController.m
//  zhihuDaily
//
//  Created by  陈聪 on 16/6/12.
//  Copyright © 2016年 cc. All rights reserved.
//

//请求url
//http://news-at.zhihu.com/api/4/theme/id

#import "MMThemeController.h"
#import "SYTheme.h"
#import "MMDailyCell.h"
#import "MJExtension.h"
#import "MMStoryModel.h"
#import "MMDetailViewController.h"
#import "MMDailyHearderView.h"
#import "MMEditorModel.h"

#import "MMEditorViewController.h"


@interface MMThemeController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;

/** 存放文字模型的数组 */
@property (nonatomic, strong) NSMutableArray *stories;
/** 存放编辑模型的数组 */
@property (nonatomic, strong) NSMutableArray *avatarArray;
@property (nonatomic, strong) MMDailyHearderView *tableHeader;
@end

@implementation MMThemeController





static NSString * const theme_reuseid = @"theme_reuseid";

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.frame = CGRectMake(0, 60, kScreenWidth, kScreenHeight-60);
        _tableView.delegate =self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 80;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MMDailyCell class]) bundle:nil] forCellReuseIdentifier:theme_reuseid];
    }
    return _tableView;
}

- (MMDailyHearderView *)tableHeader {
    if (!_tableHeader) {
        _tableHeader = [MMDailyHearderView DailyHearderView];
        _tableHeader.bounds = CGRectMake(0, 0, kScreenWidth, 48);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedHeader)];
        [_tableHeader addGestureRecognizer:tap];
    }
    return _tableHeader;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = MMRandomColor;
    // NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_theme.thumbnail]];
    // UIImage *image = [UIImage imageWithData:data];
    // [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.hidden = YES;
    //    self.title = _theme.name;
    self.sy_titleLabel.text = _theme.name;
    
    [self.sy_headerBackgroundView sd_setImageWithURL:[NSURL URLWithString:_theme.thumbnail]];
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
    [self.view addSubview:self.tableView];

    [self.view bringSubviewToFront:self.sy_header];

    self.sy_attachScrollView = self.tableView;
    self.tableView.tableHeaderView = self.tableHeader;

     self.automaticallyAdjustsScrollViewInsets = NO;

    [self loadNewData];


}

- (void)loadNewData
{
    NSString *ulrStr = [NSString stringWithFormat:@"http://news-at.zhihu.com/api/4/theme/%d",_theme.ID];
    [MMHttpTools getWithURL:ulrStr parameters:nil success:^(id json) {
        self.stories = [MMStoryModel mj_objectArrayWithKeyValuesArray:json[@"stories"]];
         NSMutableArray *avatars = [@[] mutableCopy];
        self.avatarArray = [MMEditorModel mj_objectArrayWithKeyValuesArray:json[@"editors"]];
        for (MMEditorModel *editor in self.avatarArray) {
            [avatars addObject: editor.avatar];
        }
        self.tableHeader.editorIcons = avatars;


        [self.tableView reloadData];
        [self.refreshView endRefresh];
    } failure:^(NSError *error) {

    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.stories.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MMDailyCell *cell = [tableView dequeueReusableCellWithIdentifier:theme_reuseid forIndexPath:indexPath];

    cell.storyModel = self.stories[indexPath.row];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MMDetailViewController *detailVc = [[MMDetailViewController alloc] init];
    detailVc.view.backgroundColor = [UIColor whiteColor];
    detailVc.storyModel = self.stories[indexPath.row];
    [self.navigationController pushViewController:detailVc animated:YES];


}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView.contentOffset.y <= -80) {
        [self loadNewData];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yoffset = scrollView.contentOffset.y;
    if (yoffset <= 0 && yoffset >= -90) {
        self.sy_header.height = 60-yoffset;
    } else if (yoffset < -90) {
        self.tableView.contentOffset = CGPointMake(0, -90);
    }
}


- (void)sy_back
{
 [kNotificationCenter postNotificationName:OpenDrawer object:nil];

}

- (void)setTheme:(SYTheme *)theme{
    _theme = theme;
    self.navigationItem.title = theme.name;

}


- (void)clickedHeader
{

    MMEditorViewController *editorVc = [[MMEditorViewController alloc] init];
    editorVc.editorArray = self.avatarArray;
    [self.navigationController pushViewController:editorVc animated:YES];





}


//设置tableview的分割线没有开头的间距
- (void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
